package com.heer.gymnasium.module.hobby.views;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.heer.amap.utils.ToastUtil;
import com.heer.gymnasium.R;
import com.heer.gymnasium.api.ResponseData.ApiResponse;
import com.heer.gymnasium.api.SignHelper;
import com.heer.gymnasium.api.callback.BaseCallback;
import com.heer.gymnasium.api.constants.IConstants;
import com.heer.gymnasium.base.BaseActivity;
import com.heer.gymnasium.module.hobby.beans.HobbyInfoBean;
import com.heer.gymnasium.module.hobby.beans.HobbyItemBean;
import com.heer.gymnasium.module.login.views.LoginActivity;
import com.heer.gymnasium.module.main.beans.CategoryBean;
import com.heer.gymnasium.module.my.beans.UserBean;
import com.heer.gymnasium.module.store.beans.AreaBean;
import com.heer.gymnasium.util.ImageUtil;
import com.heer.imagepick.DensityUtils;
import com.zhihuiyun.library.commonadapter.QuickTypeAdapter;
import com.zhihuiyun.library.commonadapter.ViewHolder;
import com.zhihuiyun.library.switchbutton.OnSwitchListener;
import com.zhihuiyun.library.switchbutton.SwitchButton;
import com.zhihuiyun.library.view.MyGridView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Response;

import static android.R.attr.backgroundDimEnabled;
import static android.R.attr.data;

/**
 * Created by yan
 * on 2018/2/9
 */

public class HobbyActivity extends BaseActivity {

    private static final int RES_ID_LAYOUT = R.layout.layout_hobby;
    private static final int RES_ID_SWITCH_BUTTON = R.id.layout_hobby_switch_button;
    private static final int RES_ID_HOBBY_ACTIVITY_TV = R.id.layout_hobby_activity_set_tv;
    private static final int RES_ID_HOBBY_ACTIVITY_GV = R.id.layout_hobby_activity_gv;
    private static final int RES_ID_AREA_TV = R.id.layout_hobby_area_set_tv;
    private static final int RES_ID_AREA_GV = R.id.layout_hobby_area_gv;
    private static final int RES_ID_TIME_TV = R.id.layout_hobby_time_set_tv;
    private static final int RES_ID_WEEKDAYS_AM = R.id.layout_hobby_weekdays_am_tv;
    private static final int RES_ID_WEEKDAYS_PM = R.id.layout_hobby_weekdays_pm_tv;
    private static final int RES_ID_WEEKDAYS_NIGHT = R.id.layout_hobby_weekdays_night_tv;
    private static final int RES_ID_HOLIDAY_AM = R.id.layout_hobby_holiday_am_tv;
    private static final int RES_ID_HOLIDAY_PM = R.id.layout_hobby_holiday_pm_tv;
    private static final int RES_ID_HOLIDAY_NIGHT = R.id.layout_hobby_holiday_night_tv;
    private static final int RES_ID_COMPLEATE = R.id.layout_hobby_complete_tv;
    private static final int RES_ID_WEEKDAYS = R.id.layout_hobby_weekdays_ll;
    private static final int RES_ID_HOLIDAY = R.id.layout_hobby_holiday_ll;

    private static final int RES_ID_ITEM = R.layout.item_hobby;
    private static final int RES_ID_ITEM_LL = R.id.item_hobby_ll;
    private static final int RES_ID_ITEM_IV = R.id.item_hobby_iv;
    private static final int RES_ID_ITEM_TV = R.id.item_hobby_tv;
    private Activity $this;

    private List<CategoryBean> activityData;
    private List<AreaBean> areaData;

    private QuickTypeAdapter<CategoryBean> activityAdapter;
    private QuickTypeAdapter<AreaBean> areaAdapter;

    @BindView(RES_ID_HOBBY_ACTIVITY_GV)
    MyGridView activityGridView;
    @BindView(RES_ID_AREA_GV)
    MyGridView areaGridView;

    @BindView(RES_ID_SWITCH_BUTTON)
    SwitchButton switchButton;
    @BindView(RES_ID_WEEKDAYS_AM)
    TextView weekdaysAmTextView;
    @BindView(RES_ID_WEEKDAYS_PM)
    TextView weekdaysPmTextView;
    @BindView(RES_ID_WEEKDAYS_NIGHT)
    TextView weekdaysNightTextView;
    @BindView(RES_ID_HOLIDAY_AM)
    TextView holidayAmTextView;
    @BindView(RES_ID_HOLIDAY_PM)
    TextView holidayPmTextView;
    @BindView(RES_ID_HOLIDAY_NIGHT)
    TextView holidayNightTextView;
    @BindView(RES_ID_WEEKDAYS)
    LinearLayout weekdaysLinearLayout;
    @BindView(RES_ID_HOLIDAY)
    LinearLayout holidayLinearLayout;

    private HobbyInfoBean infoBean;
    private String isReceiverMsg = "";
    private String hobby_time = "";


    public static void startActivity(Activity context) {
        Intent intent = new Intent(context, HobbyActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void init() {
        super.init();
        setContentView(RES_ID_LAYOUT);
        $this = this;
    }

    @Override
    protected void initView() {
        super.initView();
        titleUtils.setTitleText("拼单爱好");
        titleUtils.startMainActivity(this);
        activityData = new ArrayList<>();
        areaData = new ArrayList<>();
        activityAdapter = new QuickTypeAdapter<CategoryBean>(this, activityData, RES_ID_ITEM) {
            @Override
            public void bindData4View(ViewHolder holder, CategoryBean data, int type, int pos) {
                holder.setText(RES_ID_ITEM_TV, data.getTitle());
                holder.setTextColor(RES_ID_ITEM_TV, getResources().getColor(R.color.black));
                holder.setVisibility(RES_ID_ITEM_IV, View.VISIBLE);
                if (data.getIcon().contains(IConstants.BASRURL)) {

                    ImageUtil.loadUrl($this, data.getIcon(), R.drawable.ic_hobby_glof, (ImageView) holder.getView(RES_ID_ITEM_IV));
                } else {
                    ImageUtil.loadUrl($this, IConstants.BASRURL + data.getIcon(), R.drawable.ic_hobby_glof, (ImageView) holder.getView(RES_ID_ITEM_IV));
                }
            }
        };

        areaAdapter = new QuickTypeAdapter<AreaBean>(this, areaData, RES_ID_ITEM) {
            @Override
            public void bindData4View(ViewHolder holder, AreaBean data, int type, int pos) {
                holder.setText(RES_ID_ITEM_TV, data.getArea());
                holder.getView(RES_ID_ITEM_LL).setPadding(DensityUtils.dip2px($this, 10), DensityUtils.dip2px($this, 5),
                        DensityUtils.dip2px($this, 10), DensityUtils.dip2px($this, 5));
            }
        };
        activityGridView.setAdapter(activityAdapter);
        areaGridView.setAdapter(areaAdapter);

        switchButton.setOnSwitchListener(new OnSwitchListener() {
            @Override
            public void onSwitchChange(int currentStatus) {
                isReceiverMsg = currentStatus == SwitchButton.OPEN ? "1" : "2";
            }
        });
        getUserinfo();
    }

    @Override
    protected void loadData() {
        super.loadData();

    }

    @OnClick({RES_ID_HOBBY_ACTIVITY_TV, RES_ID_AREA_TV, RES_ID_TIME_TV, RES_ID_COMPLEATE})
    public void onClick(View view) {
        switch (view.getId()) {
            case RES_ID_HOBBY_ACTIVITY_TV:
                StringBuffer buffer = new StringBuffer();
                for (CategoryBean bean : activityData) {
                    buffer.append(bean.getId() + ";");
                }
                String params = "";
                if (!TextUtils.isEmpty(buffer.toString())) {
                    params = buffer.toString().substring(0, buffer.toString().length() - 1);
                }
                HobbySetActivity.startActivity($this, HobbyItemBean.HobbyType.category,
                        params);
                break;
            case RES_ID_AREA_TV:
                StringBuffer bufferArea = new StringBuffer();
                for (AreaBean bean : areaData) {
                    bufferArea.append(bean.getAdcode() + ";");
                }
                String paramsArea = "";
                if (!TextUtils.isEmpty(bufferArea.toString())) {
                    paramsArea = bufferArea.toString().substring(0, bufferArea.toString().length() - 1);
                }
                HobbySetActivity.startActivity($this, HobbyItemBean.HobbyType.area, paramsArea);
                break;
            case RES_ID_TIME_TV:
                HobbySetActivity.startActivity($this, HobbyItemBean.HobbyType.time, hobby_time);
                break;

            case RES_ID_COMPLEATE:
                saveHobby();
                break;

        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == HobbySetActivity.requestcode) {
            if (resultCode == RESULT_OK) {
                int type = data.getIntExtra(EXTRA_TYPE, 0);
                if (type == HobbyItemBean.HobbyType.time) {
                    hobby_time = data.getStringExtra(EXTRA_DATA);
                    setTimeData(hobby_time);
                } else if (type == HobbyItemBean.HobbyType.area) {
                    List<HobbyItemBean> beanList = (List<HobbyItemBean>) data.getSerializableExtra(EXTRA_DATA);
                    areaData.clear();
                    if (beanList != null) {
                        for (HobbyItemBean bean : beanList) {
                            if (bean.isSelectStatus()) {
                                AreaBean areaBean = new AreaBean(bean.getName());
                                areaBean.setAdcode(bean.getId());
                                areaData.add(areaBean);
                            }
                        }
                    }
                    areaAdapter.notifyDataSetChanged();
                } else if (type == HobbyItemBean.HobbyType.category) {
                    List<HobbyItemBean> beanList = (List<HobbyItemBean>) data.getSerializableExtra(EXTRA_DATA);
                    activityData.clear();
                    if (beanList != null) {
                        for (HobbyItemBean bean : beanList) {
                            if (bean.isSelectStatus()) {
                                CategoryBean categoryBean = new CategoryBean();
                                categoryBean.setId(bean.getId());
                                categoryBean.setIcon(bean.getIconUrl());
                                categoryBean.setTitle(bean.getName());
                                activityData.add(categoryBean);
                            }
                        }
                    }
                    activityAdapter.notifyDataSetChanged();
                }
            }
        }
    }

    //    设置time数据
    private void setTimeData(String times) {
        weekdaysAmTextView.setVisibility(View.INVISIBLE);
        weekdaysPmTextView.setVisibility(View.INVISIBLE);
        weekdaysNightTextView.setVisibility(View.INVISIBLE);
        holidayAmTextView.setVisibility(View.INVISIBLE);
        holidayPmTextView.setVisibility(View.INVISIBLE);
        holidayNightTextView.setVisibility(View.INVISIBLE);

        if (!times.contains("-1")) {
        weekdaysLinearLayout.setVisibility(View.VISIBLE);
        holidayLinearLayout.setVisibility(View.VISIBLE);
            if (times.contains("1")) {
                weekdaysAmTextView.setVisibility(View.VISIBLE);
            }
            if (times.contains("2")) {
                weekdaysPmTextView.setVisibility(View.VISIBLE);
            }
            if (times.contains("3")) {
                weekdaysNightTextView.setVisibility(View.VISIBLE);
            }
            if (times.contains("4")) {
                holidayAmTextView.setVisibility(View.VISIBLE);
            }
            if (times.contains("5")) {
                holidayPmTextView.setVisibility(View.VISIBLE);
            }
            if (times.contains("6")) {
                holidayNightTextView.setVisibility(View.VISIBLE);
            }
        }else{

          weekdaysLinearLayout.setVisibility(View.GONE);
        holidayLinearLayout.setVisibility(View.GONE);}
    }

    /**
     * 获取用户信息
     */
    public void getUserinfo() {
        SignHelper signHelper = new SignHelper("");
        mAppHelper.apiService().getuserInfo(signHelper.getToken()).enqueue(new BaseCallback<ApiResponse<UserBean>>() {
            @Override
            public void onSuccess(Call<ApiResponse<UserBean>> call, Response<ApiResponse<UserBean>> response) {
                UserBean bean = response.body().getData();
                infoBean = HobbyInfoBean.dataChange(bean);
                activityData.clear();
                areaData.clear();
                activityData.addAll(infoBean.getCategory());
                activityAdapter.notifyDataSetChanged();
                areaData.addAll(infoBean.getArea());
                areaAdapter.notifyDataSetChanged();
                hobby_time = infoBean.getTime();
                setTimeData(infoBean.getTime());
                if (infoBean.getIsAccept().equals("1")) {
                    switchButton.setCheck(true);
                } else {
                    switchButton.setCheck(false);
                }
            }
        });

    }

    private void saveHobby() {
        String hobby_type = "-1", hobby_adcode = "-1";
        StringBuffer bufferType = new StringBuffer();
        if (activityData != null && activityData.size() != 0) {
            for (CategoryBean bean : activityData) {
                bufferType.append(bean.getId() + ";");
            }
            hobby_type = bufferType.toString().substring(0, bufferType.toString().length() - 1);
        }

        StringBuffer bufferArea = new StringBuffer();
        if (areaData != null && areaData.size() != 0) {
            for (AreaBean bean : areaData) {
                bufferArea.append(bean.getAdcode() + ";");
            }
            hobby_adcode = bufferArea.toString().substring(0, bufferArea.toString().length() - 1);
        }

        mAppHelper.apiService().saveHobby(hobby_type, hobby_adcode, hobby_time, isReceiverMsg).enqueue(new BaseCallback<ApiResponse<String>>() {
            @Override
            public void onSuccess(Call<ApiResponse<String>> call, Response<ApiResponse<String>> response) {
                response.body().getData();
                finish();
            }
        });
    }
}
